package com.example.democrud.employee;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.WriteWorkbook;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.democrud.common.exception.DBRetrieveException;
import com.example.democrud.employee.dao.EmployeeMapper;
import com.example.democrud.employee.pojo.EmployeeDO;
import com.example.democrud.employee.pojo.EmployeeDTO;
import com.example.democrud.employee.pojo.EmployeeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName EmployeeServiceImpl
 * @Author Administrator
 * @Date 2020/4/22   13:52
 */
@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, EmployeeDO> implements EmployeeService {


    private final EmployeeMapper employeeMapper;
    public EmployeeServiceImpl(EmployeeMapper employeeMapper){
        this.employeeMapper = employeeMapper;
    }
    @Override
    @Transactional
    public Long create(EmployeeDTO employeeDTO) {
        EmployeeDO newEmployeeDO = new EmployeeDO();
        BeanUtils.copyProperties(employeeDTO,newEmployeeDO);
        newEmployeeDO.setEmployeeId(null);
        save(newEmployeeDO);
        return newEmployeeDO.getEmployeeId();
    }

    @Override
    public List<EmployeeVO> retrieve() {
        List<EmployeeDO> employeeDOList = (new LambdaQueryChainWrapper<EmployeeDO>(employeeMapper)).ne(EmployeeDO::getLogicDelete,1).list();
        ArrayList<EmployeeVO> employeeVOList = new ArrayList<>();
        for(EmployeeDO employeeDO : employeeDOList){
            EmployeeVO employeeVO = new EmployeeVO();
            BeanUtils.copyProperties(employeeDO,employeeVO);
            employeeVOList.add(employeeVO);
        }
        return employeeVOList;
    }

    @Override
    public EmployeeVO retrieve(Long id) {
        EmployeeDO employeeDO = getById(id);
        if(Objects.equals(employeeDO,null))
            throw new DBRetrieveException();

        EmployeeVO employeeVO = new EmployeeVO();
        BeanUtils.copyProperties(employeeDO,employeeVO);
        return employeeVO;
    }

    @Override
    public void write(OutputStream outputStream) {
        ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder();
        WriteWorkbook writeWorkbook = new WriteWorkbook();
        writeWorkbook.setOutputStream(outputStream);
        writeWorkbook.setExcelType(ExcelTypeEnum.XLSX);
        ExcelWriter excelWriter = new ExcelWriter(writeWorkbook);
        WriteSheet writeSheet = new WriteSheet();
        writeSheet.setSheetName("demo");
        WriteTable writeTable = new WriteTable();
        writeTable.setTableNo(1);
        List<List<String>> heads = new ArrayList<>();
        heads.add(Arrays.asList("id"));
        heads.add(Arrays.asList("姓名"));
        heads.add(Arrays.asList("年龄"));
        heads.add(Arrays.asList("生日"));
        writeSheet.setHead(heads);

        List<List<String>> userList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            userList.add(Arrays.asList("ID_" + i, "小明" + i, String.valueOf(i),LocalDateTime.now().toString()));
        }
        excelWriter.write(userList,writeSheet);
        excelWriter.finish();
    }

    @Override
    @Transactional
    public boolean update(EmployeeDTO employeeDTO) {
        EmployeeDO employeeDO = new EmployeeDO();
        BeanUtils.copyProperties(employeeDTO , employeeDO);
        return updateById(employeeDO);
    }


}
